package com.hdphone.zljutils.impl;

import android.content.Context;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.text.TextUtils;

import com.hdphone.zljutils.ZljUtils;
import com.hdphone.zljutils.inter.IWifiUtil;

import java.util.List;

/**
 * @date: 2021/6/2
 * @email： yzandlj@163.com
 * @author： yangzhi
 * @describe:
 */
public class WifiUtilImpl implements IWifiUtil {
    private final String TAG = getClass().getSimpleName();
    private WifiManager wifiManager;

    @Override
    public WifiInfo getConnectedWifiInfo() {
        wifiManager = (WifiManager) ZljUtils.getApp().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        return wifiManager.getConnectionInfo();
    }

    @Override
    public boolean isWifiEnabled() {
        wifiManager = (WifiManager) ZljUtils.getApp().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        return wifiManager.isWifiEnabled();
    }

    @Override
    public boolean OpenWifi() {
        wifiManager = (WifiManager) ZljUtils.getApp().getApplicationContext().getSystemService(Context.WIFI_SERVICE);

        boolean bRet = true;
        if (!wifiManager.isWifiEnabled()) {
            bRet = wifiManager.setWifiEnabled(true);
        }
        return bRet;
    }

    @Override
    public boolean closeWifi() {
        wifiManager = (WifiManager) ZljUtils.getApp().getApplicationContext().getSystemService(Context.WIFI_SERVICE);

        if (!wifiManager.isWifiEnabled()) {
            return true;
        } else {
            return wifiManager.setWifiEnabled(false);
        }
    }

    @Override
    public boolean connect(String wifiName, String password) {
        wifiManager = (WifiManager) ZljUtils.getApp().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        if (!isWifiEnabled()) {
            ZljUtils.TOAST().showShortToast("打开wifi后请再次连接");
        }

        if (!this.OpenWifi()) {
            return false;
        }

        // 状态变成WIFI_STATE_ENABLED的时候才能执行下面的语句
        while (wifiManager.getWifiState() == WifiManager.WIFI_STATE_ENABLING) {
            try {
                // 为了避免程序一直while循环，让它睡个100毫秒在检测……
                Thread.currentThread();
                Thread.sleep(100);
            } catch (InterruptedException ie) {
            }
        }

        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        WifiConfiguration config = new WifiConfiguration();
        config.allowedAuthAlgorithms.clear();
        config.allowedGroupCiphers.clear();
        config.allowedKeyManagement.clear();
        config.allowedPairwiseCiphers.clear();
        config.allowedProtocols.clear();

        // 指定对应的SSID
        config.SSID = "\"" + wifiName + "\"";

        config.preSharedKey = "\"" + password + "\"";
        config.hiddenSSID = true;
        config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
        config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
        config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
        config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
        config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
        config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
        config.status = WifiConfiguration.Status.ENABLED;

        int netID = wifiManager.addNetwork(config);
        wifiManager.disconnect();
        boolean bRet = wifiManager.enableNetwork(netID, true);
        wifiManager.reconnect();
        wifiManager.saveConfiguration();
        return bRet;
    }

    @Override
    public void connectWifiWithConfiguredNetworks(String wifiName) {
        wifiManager = (WifiManager) ZljUtils.getApp().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        List<WifiConfiguration> configuredNetworks = wifiManager.getConfiguredNetworks();
        if (configuredNetworks == null) {
            return;
        }
        for (WifiConfiguration configuration : configuredNetworks) {
            if (TextUtils.equals(configuration.SSID, "\"" + wifiName + "\"")) {
                wifiManager.disconnect();
                wifiManager.enableNetwork(configuration.networkId, true);
                wifiManager.reconnect();
                break;
            }
        }
    }
}
